package com.mybills.proj;

import java.net.URL;
import java.util.Vector;

import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.mybills.proj.pf.googlecal;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class OtherCalendars extends ListActivity{
    
	//String[] events = null;
	//String email = "";
	//String pwd = "";
   
    @Override
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  String[] events = null;
		  
		  Bundle bundle=getIntent().getExtras();
		  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
	        StrictMode.setThreadPolicy(policy);
        //email = bundle.getString("Email").toString();
        //pwd = bundle.getString("pwd").toString();
        MyBllsActivity mba = new MyBllsActivity();
        mba.setEmail(bundle.getString("email").toString());
        mba.setPwd(bundle.getString("pwD").toString());
        pf p = new pf();
        pf.googlecal add = p.new googlecal();
        System.out.println("----------------try it-----------------");
        try {
			events = add.view_calendars(mba);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e+"not fetched");
		}
         setListAdapter(new ArrayAdapter<String>(this, R.layout.category_row, events));
		  
		  ListView lv = getListView();
		  lv.setTextFilterEnabled(true);
		  //Button goBut = (Button)findViewById(R.id.button1);
    }
    public String[] view_calendars(MyBllsActivity mba) throws Exception{
		//MyBllsActivity mba = new MyBllsActivity();
		int count=0;
		String[] eve ;
		String url = mba.getEmail();
		String url1 = mba.getPwd();
		Vector<String> v = new Vector<String>();
		CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
		CalendarEntry calendar = new CalendarEntry();
		myService.setUserCredentials(url,url1);
		URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full");
		CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
		System.out.println("Calendars you own:");
		System.out.println();
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
			  count++;
			  }
		eve = new String[count];
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		  CalendarEntry entry = resultFeed.getEntries().get(i);
		  eve[i] = entry.getId().toString();
		  //System.out.println("\t" + v.elementAt(i));
		  }
		return eve;
	}
    public void new_cal(View v) throws Exception{
    	int count;
    	String[] cals = null;
    	Intent it = new Intent();
    	MyBllsActivity mba = new MyBllsActivity();
    	Bundle bundle=getIntent().getExtras();
    	mba.setEmail(bundle.getString("email").toString());
        mba.setPwd(bundle.getString("pwD").toString());
    	it.putExtra("email", bundle.getString("Email"));
        it.putExtra("pwD", bundle.getString("pwd"));
        cals = view_calendars(mba);
        count = cals.length;
        for(int i = 0 ; i < count ; i++){
        	it.putExtra("ID"+i,cals[i]);
        } 
    	//System.out.println("---------"+bundle.getString("Email").toString()+"cateogries");
    	it.setClass(this, OtherCalendars.class);
    	startActivity(it);
    }
		 
	}
	